{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第8章.模型持久化\n",
    "## 8.1.针对python对象的序列化\n",
    "<P>使用模块pickle序列化.该模块是python自带的。pickle主要提供了以下方法</P>\n",
    "<ul>\n",
    "    <li>dump(obj,file,*kwargs).该方法把python对象obj按照一定协议protocol序列化后保存到文件对象file(必须实现write()方法)</li>\n",
    "    <li>dumps(obj),把Python对象obj按照一定协议protocol序列化为一个字节对象,也就是本方法的返回值</li>\n",
    "    <li>load(file)，从一个序列化文件对象file(必须实现了read()和readlinne()方法)中加载数据</li>\n",
    "    <Li>loads(data).从序列化后的数据对象data中加载并反序列化为python对象</Li>\n",
    "</ul>"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测结果 [0]\n",
      "精度 0.973\n",
      "-------------------------------------\n",
      "预测结果 [0]\n",
      "预测精度 0.973\n"
     ]
    }
   ],
   "source": [
    "from sklearn import svm\n",
    "from sklearn import datasets\n",
    "\n",
    "# 训练一个SVC()模型\n",
    "clf = svm.SVC()\n",
    "X,y = datasets.load_iris(return_X_y=True)\n",
    "clf.fit(X,y) # 训练模型\n",
    "# 导入pickle\n",
    "import pickle\n",
    "# 1. 保存为python对象\n",
    "# 使用模块pickle的dumps()方法导出(保存)模型\n",
    "model = pickle.dumps(clf)\n",
    "# 使用模块pickle的loads()方法加载模型\n",
    "# unpickling\n",
    "clf2 = pickle.loads(model)\n",
    "# 应用模型\n",
    "y0 = clf2.predict(X[0:1])\n",
    "print(\"预测结果\",y0)\n",
    "print(\"精度\",round(clf2.score(X,y),3))\n",
    "print(\"-\"*37)\n",
    "\n",
    "# 2.保存为文件\n",
    "# 获取持久化模型(保存的文件对象)\n",
    "outFile, inFile = None,None\n",
    "\n",
    "try:\n",
    "    outFile = open(\"F:\\\\sklearn机器学习高级进阶\\\\SVC_Model.clf\",mode=\"wb\")\n",
    "    pickle.dump(clf,outFile)\n",
    "except Exception as e:\n",
    "    # 处理错误过程\n",
    "    print(\"Error\",e)\n",
    "finally:\n",
    "    if outFile:\n",
    "        outFile.close()\n",
    "\n",
    "try:\n",
    "    inFile = open(\"F:\\\\sklearn机器学习高级进阶\\\\SVC_Model.clf\",mode=\"rb\")\n",
    "    clf3 = pickle.load(inFile)\n",
    "except Exception as e:\n",
    "    # 处理错误过程\n",
    "    print(\"Error\",e)\n",
    "finally:\n",
    "    if inFile:\n",
    "        inFile.close()\n",
    "\n",
    "# 应用模型\n",
    "y0 = clf3.predict(X[0:1])\n",
    "print(\"预测结果\",y0)\n",
    "print(\"预测精度\",round(clf3.score(X,y),3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-15T03:01:46.661960800Z",
     "start_time": "2023-09-15T03:01:46.627966100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "精度 0.973\n"
     ]
    }
   ],
   "source": [
    "outFile2 = open(\"F:\\\\sklearn机器学习高级进阶\\\\SVC_Model.clf\",mode='rb')\n",
    "clf4=pickle.load(outFile2)\n",
    "score=clf4.score(X,y)\n",
    "print(\"精度\",round(score,3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-09-15T03:03:53.661405100Z",
     "start_time": "2023-09-15T03:03:53.637365Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
